package david.java.tools;

import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description:
 * @Author: ZhaoDawei
 * @Date: Create in 8:15 下午 2022/7/20
 */
public abstract class BaseUtil {
    public static void main(String[] args) throws IOException {
        System.out.println(blankFilter("haha", 10));
    }

    public static void mkdirIfNotExists(String path) {
        File file = new File(path);
        String parent = file.getParent();
        if (parent.equals("/")) {
            return;
        }
        boolean exists = file.getParentFile().exists();
        if (!exists) {
            file.getParentFile().mkdirs();
        }
    }

    public static List<String> readFromPath(String path) throws IOException {
        List<String> list = new ArrayList<>();
        BufferedReader br = new BufferedReader(new FileReader(path));
        String line;
        while ((line = br.readLine()) != null) {
            list.add(line);
        }

        br.close();
        return list;
    }

    public static void writeToPath(String path,String lines) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        bw.write(lines);
        bw.flush();
        bw.close();
    }

    public static void writeToPath(String path,List<String> lines) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        for (String line: lines) {
            bw.write(line);
            bw.newLine();

        }
        bw.flush();
        bw.close();
    }

    public static String getColFromDDl(String line) {
        if (StringUtils.isNotBlank(line)) {
            String[] split = line.split("\\s");
            inner:
            for (String str : split) {
                // 过滤注释
                if (str.startsWith("--")) {
                    break inner;
                }
                if (StringUtils.isNotBlank(str)) {
                    char[] chars = str.toCharArray();
                    StringBuilder sb = new StringBuilder();
                    for (char c : chars) {
                        if (c != '`' && c != ',') {
                            sb.append(c);
                        }
                    }
                    return sb.toString();
                }
            }
        }
        return null;
    }

    /**
     * 功能描述: 从DDl 或 列中抽取列名
     * @param
     * @return:
     */
    public static List<String> extractColFromDDLOrCol(String path, boolean backQuote) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(path));
        List<String> result = new ArrayList<>();

        String line;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isNotBlank(line)) {
                String[] split = line.split("\\s");
                inner:
                for (String str : split) {
                    // 过滤注释
                    if (str.startsWith("--")) {
                        break inner;
                    }
                    if (StringUtils.isNotBlank(str)) {
                        char[] chars = str.toCharArray();
                        StringBuilder sb = new StringBuilder();
                        for (char c : chars) {
                            if (c != '`' && c != ',') {
                                sb.append(c);
                            }
                        }
                        if(backQuote) {
                            result.add(backQuote(sb.toString()));
                        }else {
                            result.add(sb.toString());
                        }
                        break inner;
                    }
                }
            }
        }

        br.close();
        return result;
    }

    public static List<String> extractColFromDDLOrCol(String path) throws IOException {
        return extractColFromDDLOrCol(path, false);
    }

    private static String backQuote(String str) {
        return "`" + str + "`";
    }

    public static String leftPad(String str, int n) {
        return StringUtils.leftPad("", n, str);
    }

    public static String leftPad(int n) {
        return StringUtils.leftPad("", n, " ");
    }

    public static String blankFilter(String str, int maxColLength) {
        int index = maxColLength / 8 * 8 + 8;
        int length = str.length();
        return leftPad(index - length);
    }

    public static String leftBlankFilter2Length(String str, int length) {
        return str + leftPad(length - str.length());
    }

}
